<?php
include_once dirname(__FILE__) . '/profile.class.php';
ini_set('memory_limit', '-1');

class ImportForms extends Main
{
	public $_res;
	
	##########################################################################################################
	# IMAGE FUNCTIONS																						 #
	# You do not need to alter these functions																 #
	##########################################################################################################
	function resizeImage($image,$width,$height,$scale) {
		list($imagewidth, $imageheight, $imageType) = getimagesize($image);
		$imageType = image_type_to_mime_type($imageType);
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		switch($imageType) {
			case "image/gif":
				$source=imagecreatefromgif($image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				$source=imagecreatefromjpeg($image); 
				break;
			case "image/png":
			case "image/x-png":
				$source=imagecreatefrompng($image); 
				break;
		}
		imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
		
		switch($imageType) {
			case "image/gif":
				imagegif($newImage,$image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				imagejpeg($newImage,$image,90); 
				break;
			case "image/png":
			case "image/x-png":
				imagepng($newImage,$image);  
				break;
		}
		imagedestroy($newImage);

		chmod($image, 0777);
		return $image;
	}

	
	//You do not need to alter these functions
	function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
		list($imagewidth, $imageheight, $imageType) = getimagesize($image);
		$imageType = image_type_to_mime_type($imageType);
		
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		switch($imageType) {
			case "image/gif":
				$source=imagecreatefromgif($image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				$source=imagecreatefromjpeg($image); 
				break;
			case "image/png":
			case "image/x-png":
				$source=imagecreatefrompng($image); 
				break;
		}
		imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
		switch($imageType) {
			case "image/gif":
				imagegif($newImage,$thumb_image_name); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				imagejpeg($newImage,$thumb_image_name,90); 
				break;
			case "image/png":
			case "image/x-png":
				imagepng($newImage,$thumb_image_name);  
				break;
		}
		chmod($thumb_image_name, 0777);
		return $thumb_image_name;
	}
	//You do not need to alter these functions
	function getHeight($image) {
		$size = getimagesize($image);
		$height = $size[1];
		return $height;
	}
	//You do not need to alter these functions
	function getWidth($image) {
		$size = getimagesize($image);
		$width = $size[0];
		return $width;
	}
	##########################################################################################################
	public function ReadImportList()
	{
		$sql = "SELECT id, dt_import, num_import, user_import FROM import_forms ";
		$sql .= "ORDER BY dt_import DESC";
		
		$this->_res = mysql_query ($sql, $this->conn);
		//die($sql);
		include '../template/' . TEMPLATE_ADMIN_NAME . '/import_forms.php';
		return;
	}
	public function ImportProfilesFromForms()
	{
		try
		{
			mysql_query("BEGIN", $this->conn);
			$num_import = 0;
			$ora=date("H:i");
			//echo "ORA ".$ora;
			//Pulisco la tabella dagli spam
			$sql="delete from ft_form_5 where (col_1 is null or col_1 ='') AND (col_3 is null or col_3 ='')";
			
			mysql_query($sql,$this->conn);
			
			$sql="SELECT * from ft_form_5 where submission_id>=756 And is_finalized = 'yes' AND is_migrated = 'N' AND col_1 <>'' AND col_3  <>'' AND (col_31 is null or col_31 = '') AND col_51 = 'si'"; //col_31 = pubblicato, col_51 = importa
			
			$result=mysql_query($sql, $this->conn) or die("Query failed");
			
			if (mysql_num_rows($result)>0)
			{
				$objUser = new NewUser();
				while ($row = mysql_fetch_array($result))
				{
					$nome = trim(mysql_real_escape_string($row["col_1"]));
					$username = strtolower(str_replace(" ", "-", $nome)); //sostituisce gli spazi con i trattini
					$username_new = $username;
					$i=1;
					
					$sql2="select username from utenti where LOWER(username)='".$username."'";
					$result2=mysql_query($sql2, $this->conn) or die("Query failed");
					/*if (mysql_num_rows($result2)>=1)
					{		
						$i++;
						$username = $username.$i;					
					}	
						
					*/
					while (mysql_num_rows($result2)>=1)
					{		
						$i++;
						$username_new = $username.$i;
						mysql_free_result($result2);
						$sql2="select username from utenti where LOWER(username)='".$username_new."'";
						$result2=mysql_query($sql2, $this->conn) or die("Query failed");						
					}	
						
					mysql_free_result($result2);
					
					$password = md5($username_new.'2013');
					
					if($row["col_2"]=="Donna" ) {
						$sesso = 'D';				
						$sesso_annuncio = 'Escort';						
					}	
					else if($row["col_2"]=="Trans" ) {
						$sesso = 'T';		
						$sesso_annuncio = 'Trans';						
					}	
					else if($row["col_2"]=="Uomo" ) {
						$sesso = 'U';			
						$sesso_annuncio = 'Accompagnatore';					
					}	
					else {
						$sesso = 'D';		
						$sesso_annuncio = 'Escort';						
					}	
						
					$mail = $row["col_3"];
			
					$submission_id=$row["submission_id"];
					$ip_address=$row["ip_address"];
					$cellulare=$row["col_4"];
					$prefisso=substr($cellulare, 0, 3);
					$numero=substr($cellulare, 3);
					$dt_nascita=$row["col_5"];
					$tipo_class=$row["col_32"];
					if($tipo_class=="" || $tipo_class==null)
						$tipo_class="S"; //Smart
						
					if($dt_nascita!=null && $dt_nascita!="")
					{	
						$obj = new Main();
						$dt_nascita = $obj->formattaDataPerQuery($dt_nascita);
					}		
					else
						$dt_nascita="";
						
					$citta_nome=trim(strtolower(mysql_real_escape_string($row["col_6"])));
					$cap=$row["col_7"];
					$zona=mysql_real_escape_string($row["col_8"]);
					$altezza=$row["col_9"]=="" ? 0 : $row["col_9"];
					$peso=$row["col_10"]=="" ? 0 : $row["col_10"];
					$seno=$row["col_11"]=="" ? 0 : $row["col_11"];
					$nazionalita_nome=strtolower(mysql_real_escape_string($row["col_12"]));
					$annuncio=mysql_real_escape_string($row["col_13"]);
					$annuncio_eng=mysql_real_escape_string($row["col_45"]);
					$annuncio_fra=mysql_real_escape_string($row["col_46"]);
					$annuncio_esp=mysql_real_escape_string($row["col_47"]);
					$annuncio_por=mysql_real_escape_string($row["col_48"]);
					$annuncio_deu=mysql_real_escape_string($row["col_49"]);
					$annuncio_cin=mysql_real_escape_string($row["col_50"]);
					$capelli=$row["col_33"];
					$occhi=$row["col_34"];
					$orario_da=$row["col_14"];
					$orario_a=$row["col_15"];
					$giorno_da=$row["col_35"];
					$giorno_a=$row["col_36"];
					$f_disp_2_piu = is_null($row["col_37"]) ? "N" : "S";
					$tipo_servizi=$row["col_16"];
					$f_disp_uomini = is_null($row["col_17"]) ? "N" : "S";
					$f_disp_donne = is_null($row["col_18"]) ? "N" : "S";
					$f_disp_coppie = is_null($row["col_19"]) ? "N" : "S";
					$data=date("Y-m-d");
					$regdate = time();
					
					$sql2="select id_citta from citta where LOWER(nome)='".$citta_nome."'";
					$result2=mysql_query($sql2, $this->conn) or die("Query failed");
					if (mysql_num_rows($result2)==1)
					{		
						$row2 = mysql_fetch_array($result2);
						$id_citta=$row2["id_citta"];
					}		
					else
						$id_citta = 0;
						
					mysql_free_result($result2);
							
					$sql2="select id_nazionalita from nazionalita where LOWER(nome)='".$nazionalita_nome."'";
					$result2=mysql_query($sql2, $this->conn) or die("Query failed");
					if (mysql_num_rows($result2)==1)
					{		
						$row2 = mysql_fetch_array($result2);
						$id_nazionalita=$row2["id_nazionalita"];
					}		
					else
						$id_nazionalita = 0;
						
					mysql_free_result($result2);
							
					$key_control = $objUser->GetKey(40);
					
					$sql2  = "INSERT INTO utenti (";
					$sql2 .= "nome, username, password, tipo_utente, sesso, capelli, occhi, mail, prefisso1, telefono1, id_citta, id_nazionalita, zona, f_attivo, f_attivo_admin, regdate, f_confermato, key_control, ";
					$sql2 .= "tipo_class, dt_nascita, cap, indirizzo, altezza, peso, seno, annuncio, orario_da, orario_a, giorno_da, giorno_a, tipo_servizi, IP, f_reg_admin, ";
					$sql2 .= "f_disp_uomini, f_disp_donne, f_disp_coppie, f_disp_2_piu, f_importato, form_id) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "'$nome', '$username_new', '$password', 3, '$sesso', '$capelli', '$occhi', '$mail', '$prefisso', '$numero', $id_citta, $id_nazionalita, '$zona', 'S', 'S', $regdate, 'N', '$key_control', ";
					$sql2 .= "'$tipo_class', '$dt_nascita', '$cap', '', $altezza, $peso, $seno, '$annuncio', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a', '$tipo_servizi', '$ip_address', 'S', ";
					$sql2 .= "'$f_disp_uomini', '$f_disp_donne', '$f_disp_coppie', '$f_disp_2_piu', 'S', $submission_id";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);
					$user_id = mysql_insert_id();

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in inglese
					$sql2  = "INSERT INTO utenti_en (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_eng', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in francese
					$sql2  = "INSERT INTO utenti_fr (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_fra', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in spagnolo
					$sql2  = "INSERT INTO utenti_es (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_esp', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in portoghese
					$sql2  = "INSERT INTO utenti_po (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_por', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in tedesco
					$sql2  = "INSERT INTO utenti_de (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_deu', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					//Annuncio in cinese
					$sql2  = "INSERT INTO utenti_ci (";
					$sql2 .= "user_id, sesso, altezza, peso, seno, capelli, occhi, annuncio, orario_da, orario_a, giorno_da, giorno_a) ";
					$sql2 .= "VALUES ( ";
					$sql2 .= "$user_id, '$sesso', $altezza, $peso, $seno, '$capelli', '$occhi', '$annuncio_cin', '$orario_da', '$orario_a', '$giorno_da', '$giorno_a'";
					$sql2 .= ")";		
					//die($sql2);
					$result2 = mysql_query($sql2, $this->conn);

					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql2;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					$num_import = $num_import+1;
					
					$sql = "UPDATE ft_form_5 SET is_migrated = 'S', col_31 = 'pubblicato' WHERE submission_id = $submission_id";
					//die($sql);
					$result2 = mysql_query($sql, $this->conn);
					if(!$result2)
					{
						$message  = 'Invalid query: ' . mysql_error() . "\n";
						$message .= 'Whole query: ' . $sql;
						mysql_query("ROLLBACK", $this->conn);
						die($message);
					}
					
					//Carico le immagini					
					$upload_path = realpath("..")."/images/users/";	
					$upload_path_box = realpath("..")."/images/users/box/";	
					// Max width allowed for the large image
					$max_height = "600";
					$thumb_width = "360";						// Width of thumbnail image
					$thumb_height = "450";						// Height of thumbnail image
					$upload_dir = "images/users/"; 				// The directory for the images to be saved in
					$upload_box_dir = "images/users/box/"; 		// The directory for the images box to be saved in
					$random_key = strtotime(date('Y-m-d H:i:s')); //assign the timestamp to the session variable
					$n = 0;
					$n++;
					$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
					$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
					
					$img1=$row["col_20"];
					$img2=$row["col_21"];
					$img3=$row["col_22"];
					$img4=$row["col_23"];
					$img5=$row["col_24"];
					$img6=$row["col_25"];
					$img7=$row["col_26"];
					$img8=$row["col_27"];
					$img9=$row["col_38"];
					$img10=$row["col_39"];
					$img11=$row["col_40"];
					$img12=$row["col_41"];
					$img13=$row["col_42"];
					$img14=$row["col_43"];
					$img15=$row["col_44"];
					
					$x1 = 7;
					$y1 = 8;
					$x2 = 291;
					$y2 = 369;
					$w = 284;
					$h = 361;
					
					if($img1 !=null)
					{
						$file_ext = strtolower(substr($img1, strrpos($img1, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$img1, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$img1...\n");
						}
						/*if (!copy("../formtools/upload/".$img1, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$img1...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'S')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
						$publicdate = time();
						$sql = "UPDATE utenti SET publicdate='$publicdate' WHERE user_id = ".$user_id;								
						mysql_query($sql, $this->conn);	
					}
					
					if($img2 !=null)
					{
						$foto = $img2;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img3 !=null)
					{
						$foto = $img3;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					if($img4 !=null)
					{
						$foto = $img4;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					if($img5 !=null)
					{
						$foto = $img5;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					if($img6 !=null)
					{
						$foto = $img6;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					if($img7 !=null)
					{
						$foto = $img7;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					if($img8 !=null)
					{
						$foto = $img8;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img9 !=null)
					{
						$foto = $img9;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img10 !=null)
					{
						$foto = $img10;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img11 !=null)
					{
						$foto = $img11;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img12 !=null)
					{
						$foto = $img12;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img13 !=null)
					{
						$foto = $img13;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img14 !=null)
					{
						$foto = $img14;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
					if($img15 !=null)
					{
						$foto = $img15;
						$n++;
						$random_key = strtotime(date('Y-m-d H:i:s')); 
						$large_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n;
						$thumb_image_name = $nome."-".$sesso_annuncio."-".$citta_nome."-".$random_key.$n; 
						
						$file_ext = strtolower(substr($foto, strrpos($foto, '.') + 1));
						$large_image_name = $large_image_name.".".$file_ext;
						$thumb_image_name = $thumb_image_name.".".$file_ext;
						$large_image_location = $upload_path.$large_image_name;
						$thumb_image_location = $upload_path_box.$thumb_image_name;
						
						if (!copy("../formtools/upload/".$foto, "../".$upload_dir.$large_image_name)) {
							die("failed to copy ../formtools/upload/$foto...\n");
						}
						/*if (!copy("../formtools/upload/".$foto, "../".$upload_box_dir.$thumb_image_name)) {
							die("failed to copy box../formtools/upload/$foto...\n");
						}*/
						
						$width = $this->getWidth($large_image_location);
						$height = $this->getHeight($large_image_location);
						if ($height > $max_height){
							$scale = $max_height/$height;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}else{
							$scale = 1;
							$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);
						}
						
						//CREO LA THUMB
						$scale = $thumb_width/$w;
						$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
	
						//echo($uploaded);
						$sql = "INSERT INTO foto (user_id, nome_foto, ordine, f_attiva, f_principale) VALUES (".$user_id.", '$large_image_name', 1, 'S', 'N')";								
						//die($sql); 			
						$result2 = mysql_query($sql, $this->conn);
						if(!$result2)
						{
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $sql;
							mysql_query("ROLLBACK", $this->conn);
							die($message);
						}
					}
					
				}
				
			}
					
			$sql3  = "INSERT INTO import_forms (";
			$sql3 .= "dt_import, num_import, user_import) ";
			$sql3 .= "VALUES ( ";
			$sql3 .= "now(), $num_import, $_SESSION[user_id])";		
			
			$result2 = mysql_query($sql3, $this->conn);
			if(!$result2)
			{
				$message  = 'Invalid query: ' . mysql_error() . "\n";
				$message .= 'Whole query: ' . $sql3;
				mysql_query("ROLLBACK", $this->conn);
				die($message);
			}
			mysql_query("COMMIT", $this->conn);
			mysql_free_result($result);
			mysql_close($this->conn);
		}
		catch (Exception $e)
		{	
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $sql;
			mysql_query("ROLLBACK", $this->conn);
			mysql_free_result($result);
			mysql_close($conn);
			die($message);
			//header("Location: /admin/profiles.php?alert=35");					
			return;
		}
		header("Location: /admin/import_forms.php");		
	}
	

	public function NumProfilesToImport($autorizzati)
	{
		$sql="SELECT COUNT(*) as num_profiles from ft_form_5 where submission_id>=756 And is_finalized = 'yes' AND is_migrated = 'N' AND col_1 <>'' AND col_3  <>'' AND (col_31 is null or col_31 = '')";
		
		if($autorizzati== "no")
			$sql .= " AND col_51 is null";
		else if($autorizzati== "si")	
			$sql .= " AND col_51 = 'si'";
			
		$result = mysql_query ($sql, $this->conn);		
		$row = mysql_fetch_array($result);
		
		$num_profiles = $row['num_profiles'];
		mysql_free_result($result);
		return $num_profiles;
	}
	public function CompositeContent()
	{
		$page = basename($_SERVER['PHP_SELF']);

		switch($page)
		{
			case "import_forms.php":				
			$profile = new ImportForms();
			$profile->ReadImportList();
			break;
		}
	}
}
?>